package com.guit.hrms.service.impl;

import com.guit.hrms.dto.academy.AddAcademyDTO;
import com.guit.hrms.mapper.AcademyMapper;
import com.guit.hrms.mapper.EmployeeMapper;
import com.guit.hrms.mapper.PositionMapper;
import com.guit.hrms.mapper.SettingsMapper;
import com.guit.hrms.po.academy.Academy;
import com.guit.hrms.po.position.Position;
import com.guit.hrms.service.AcademyService;
import com.guit.hrms.vo.academy.AcademyVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.BeanUtils;

import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class AcademyServiceImpl implements AcademyService {

    @Autowired
    private AcademyMapper academyMapper;
    @Autowired
    private PositionMapper positionMapper;
    @Autowired
    private SettingsMapper settingsMapper;
    @Autowired
    private EmployeeMapper employeeMapper;

    /**
     * 新增二级学院
     *
     * @param addAcademyDTO
     * @return
     */
    @Override
    public void addAcademy(AddAcademyDTO addAcademyDTO) {

        Academy academy = Academy.builder()
                .academyNumber(addAcademyDTO.getAcademyNumber())
                .academyParentId(addAcademyDTO.getAcademyParentId())
                .academyName(addAcademyDTO.getAcademyName())
                .academyManagerId(addAcademyDTO.getAcademyManagerId())
                .academyProfile(addAcademyDTO.getAcademyProfile())
                .academyLevel(addAcademyDTO.getAcademyLevel())
                .createTime(LocalDateTime.now())
                .updateTime(LocalDateTime.now())
                .build();
        academyMapper.addAcademy(academy);

    }

    /**
     * 查询所有二级学院
     *
     * @return
     */
    @Override
    public List<AcademyVO> getAcademy() {
        List<Academy> academyList = academyMapper.getAcademyWithPositions();
        
        return academyList.stream().map(academy -> {
            AcademyVO academyVO = new AcademyVO();
            BeanUtils.copyProperties(academy, academyVO);
            
            // 获取该二级学院的所有岗位
            List<Position> positions = academyMapper.getPositionsByAcademyId(academy.getAcademyId());
            academyVO.setPositions(positions);
            
            return academyVO;
        }).collect(Collectors.toList());
    }

    /**
     * 更新二级学院
     *
     * @param academy
     * @return
     */
    @Override
    public void updateAcademy(Academy academy) {

        academy.setUpdateTime(LocalDateTime.now());

        academyMapper.updateAcademy(academy);
    }

    /**
     * 批量删除二级学院
     *
     * @param academyIds
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteAcademy(List<Integer> academyIds) {
        try {
            //获取设置
            String setting_name = "部门是否连带删除";
            Integer value = settingsMapper.getValueBySettingName(setting_name);

            // 如果设置值为空，默认使用不连带删除的模式
            int deleteMode = (value != null) ? value : 0;

            //根据id删除二级学院
            if (deleteMode == 0) {
                // 仅删除指定的学院
                for (Integer academyId : academyIds) {
                    academyMapper.deleteAcademyById(academyId);
                }
            } else if (deleteMode == 1) {
                // 连带删除子部门和相关数据
                for (Integer academyId : academyIds) {
                    // 删除主学院
                    academyMapper.deleteAcademyById(academyId);
                    // 删除子部门
                    academyMapper.deleteAcademyByParentId(academyId);
                    // 删除子岗位
                    positionMapper.deletePositionByDeptId(academyId);
                    // 修改员工所属
                    employeeMapper.setDeptAndPositionByDeptId(academyId);
                }
            }
        } catch (Exception e) {
            throw new RuntimeException("删除二级学院失败: " + e.getMessage());
        }
    }
}
